package com.ruoyi.patrol.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.patrol.domain.*;
import com.ruoyi.patrol.domain.vo.PtSiteRecordVo;
import com.ruoyi.patrol.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 手机端操作Controller
 * 
 * @author mlb
 * @date 2022-11-14
 */
@RestController
@RequestMapping("/patrol/line/app")
public class PtLineAppController extends BaseController
{
    @Autowired
    private IPtLineAppService ptLineAppService;
    @Autowired
    private IPtLineRecordDetailService ptLineRecordDetailService;
    @Autowired
    private IPtSiteRecordStaffService ptSiteRecordStaffService;

    /**
     * 查询巡查线路（及巡查结果）列表
     */

    /** @PreAuthorize("@ss.hasPermi('patrol:line:app:list')")*/
    @GetMapping("/list")
    public TableDataInfo list(PtLine ptLine)
    {
        startPage();
        List<PtLineApp> list = ptLineAppService.selectPtLineList(ptLine);
        return getDataTable(list);
    }

    /**
     * 获取巡查线路详细信息
     */
    /** @PreAuthorize("@ss.hasPermi('patrol:line:query')") */
    @GetMapping(value = { "{id}"})
    public AjaxResult getInfo(@PathVariable(value = "id") Long id)
    {
        return AjaxResult.success(ptLineAppService.selectPtLineById(id));
    }

    /**
     * 新增巡逻点位打卡信息
     */
    /** @PreAuthorize("@ss.hasPermi('patrol:line:add')") */
    @Log(title = "巡逻点位打卡", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PtSiteRecordVo record)
    {
        if (SecurityUtils.getGuardId() != null) {
            return toAjax(ptLineRecordDetailService.updatePtLineRecordDetail(record));
        } else if (SecurityUtils.getStaffId() != null) {
            return toAjax(ptSiteRecordStaffService.updatePtSiteRecordStaff(record));
        }
        return AjaxResult.success("操作成功");
    }

    /**
     * 查询巡查线路记录列表
     */
    @GetMapping("/record")
    public TableDataInfo getDetailByScheduleId(Long scheduleId)
    {
        List<?> list = null;
        if (SecurityUtils.getGuardId() != null) {
            PtLineRecordDetail query = new PtLineRecordDetail();
            query.setScheduleId(scheduleId);
            //query.setUpdateBy(SecurityUtils.getNickName());

            startPage();
            list = ptLineRecordDetailService.selectPtLineRecordDetailList(query);
        } else if (SecurityUtils.getStaffId() != null) {
            PtSiteRecordStaff query = new PtSiteRecordStaff();
            query.setScheduleId(scheduleId);
            //query.setUpdateBy(SecurityUtils.getNickName());

            startPage();
            list = ptSiteRecordStaffService.selectPtSiteRecordStaffList(query);
        }


        return getDataTable(list);
    }
}
